home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Unix / maclayersunixend1.30.shar / 1.30 / layersize.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  4KB  |  161 lines

  1. /*             
  2.  *    layersize.c
  3.  *
  4.  *        update Unix side with window size information
  5.  *
  6.  *             Copyright (C) 1993 by Eric C. Rosen
  7.  *             Copyright (C) 1989-1992 by David W. Trissel
  8.  *
  9.  *    All rights reserved.
  10.  *
  11.  *  Not derived from licensed software.
  12.  *
  13.  * Permission is granted to freely use, copy, modify, and redistribute
  14.  * this software, provided that no attempt is made to gain profit from it,
  15.  * the author is not construed to be liable for any results of using the
  16.  * software, alterations are clearly marked as such, and this notice is
  17.  * not modified.
  18.  *
  19.  *
  20.  *    NOTE: MacLayers WILL AUTOMATICALLY UPDATE THE UNIX SIDE layers PROGRAM
  21.  *          WHEN YOU RESIZE YOUR WINDOWS. YOU ONLY NEED TO USE THIS PROGRAM
  22.  *          IF YOU RESIZE YOUR WINDOWS AFTER LOGGING TO A REMOTE HOST, OR IF
  23.  *           YOU ARE NOT RUNNING layers. SEE DOCUMENTATION FOR DETAILS.
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <sys/ioctl.h>
  29. #ifdef SVR4
  30. #include <sys/termios.h>
  31. #endif
  32.  
  33.  
  34. extern int sys_nerr;
  35. extern char    *sys_errlist[];
  36.  
  37. static void gotsyserr(/* char * */);
  38. static void goterr(/* char * */);
  39. static int    getnumber(/* char * */);
  40.  
  41.  
  42.                         /* main() - update BSD window size */
  43.  
  44. main(ac, av)
  45. int            ac;                            /* argument count */
  46. char        **av;                        /* argument vector */
  47. {
  48.     struct winsize wsize;                /* window size structure for ioctl() */
  49.     char        *ap;                    /* argument scan pointer */
  50.     int            lines;                    /* new lines value */
  51.     int            cols;                    /* new columns value */
  52.  
  53.     if (--ac != 2)
  54.         goterr("Missing lines and column options");
  55.  
  56.     /* get window size (actually do this to set xpixel and ypixel values) */
  57.     if (ioctl(0, TIOCGWINSZ, &wsize) == -1)
  58.         gotsyserr("No window support in host"); /* terminate with message */
  59.  
  60.     /* scan looking for -l and -c line and column numeric sizes */
  61.     lines = cols = 0;                    /* reset values */
  62.     while (ac > 0)
  63.       {    ap = *++av;                        /* point to next argument string */
  64.         if (ac-- > 0 && *ap == '-')        /* if option ... */
  65.         switch (ap[1])
  66.         { case 'l':        /* lines */
  67.             lines = getnumber(&ap[2]);
  68.             break;
  69.  
  70.           case 'c':        /* columns */
  71.             cols = getnumber(&ap[2]);
  72.             break;
  73.  
  74.           default:
  75.             goterr("Unsupported option"); /* unsupported option */
  76.             break;
  77.  
  78.         } /* end '-' argument */
  79.         else
  80.             goterr("Unsupported parameter"); /* unsupported parameter */
  81.  
  82.       } /* end while argument vector scan */
  83.             
  84.     /* must have both lines and columns */
  85.     if (lines == 0 || cols == 0)
  86.         goterr("Must specify both lines and columns");
  87.  
  88.     wsize.ws_col = cols;                /* set columns */
  89.     wsize.ws_row = lines;                /* set lines */
  90.     /* update the kernel */
  91.     if (ioctl(0, TIOCSWINSZ, &wsize) == -1)
  92.         gotsyserr("Failed to update window size"); /* didn't go */
  93.  
  94.  
  95.  
  96.                 /* goterr() - issue error and terminate */
  97.  
  98. static void
  99. goterr(msg)
  100. char        *msg;                        /* error message string */
  101. {
  102.     printf("%s\n", msg);                /* send error message to user */
  103.     exit(1);                            /* terminate with error */
  104.  
  105. } /* goterr() */
  106.  
  107.  
  108.                 /* gotsyserror() - system error return */
  109.  
  110. static void
  111. gotsyserr(msg)
  112. char        *msg;                        /* error string */
  113. {
  114.     if (errno > 0 && errno < sys_nerr)
  115.         printf("%s: %s\n", msg, sys_errlist[errno]);
  116.     else
  117.         printf("%s: Error %d\n", msg, errno);
  118.  
  119.     exit(1);                            /* exit with failure */
  120.  
  121. } /* gotsyserr() */
  122.  
  123.  
  124.                     /* getnumber() - parse option number */
  125.  
  126. static int
  127. getnumber(str)
  128. char        *str;                        /* start of option string */
  129. {
  130.     int            n;                        /* number being built */
  131.  
  132.     if (str == NULL)
  133.         goterr("Invalid numeric in option");
  134.  
  135.     /* skip any leading delimiters */
  136.     while (*str && (*str == ' ' || *str == '\t'))
  137.         str++;
  138.  
  139.     for(n=0; *str && *str >= '0' && *str <= '9'; str++)
  140.         n = n*10 + *str - '0';            /* add next digit in */
  141.  
  142.     /* make sure number terminates legally */
  143.     switch (*str)
  144.     { case '\0':
  145.       case ' ':
  146.       case '\t':
  147.       case '\n':
  148.         if (n <= 0 || n > 200)
  149.             goterr("Number out of range");
  150.         break;                            /* these are OK */
  151.  
  152.       default:
  153.         goterr("Invalid numeric in option");
  154.  
  155.     } /* end switch */
  156.  
  157.     return ( n );                        /* return the number */
  158.  
  159. } /* getnumber() */
  160.